package com.longrise.android.byjk.plugins.dealsituation.cachecourse;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.model.DownloadedManageBean;
import com.longrise.common.utils.PrintLog;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.download.ImageDownloader;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xkj on 2016/3/13.
 * 缓存管理的adpter
 */
public class DownloadedManageAdapter extends BaseAdapter {

    private static final String TAG = "DownloadedManageAdapter";
    private Context mContext;
    private List<DownloadedManageBean> mList = new ArrayList<>();

    public DownloadedManageAdapter(Context context){
        this.mContext = context;
    }

    public void setData(List<DownloadedManageBean> list){
        this.mList.clear();
        mList.addAll(list);
    }

    @Override
    public int getCount() {
        if(mList!=null){
            return mList.size();
        }else{
            return 0;
        }
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
       Holder holder = null;
        if(convertView==null){
            convertView = View.inflate(mContext,R.layout.item_downloadedmanage,null);
            holder = new Holder();
            holder.mIv = (ImageView) convertView.findViewById(R.id.item_downloadedmanage_iv);
            holder.mTvTitle = (TextView) convertView.findViewById(R.id.item_downloadedmanage_tv1);
            holder.mTvTeacher = (TextView) convertView.findViewById(R.id.item_downloadedmanage_money);
            holder.mTvNumdownloaded = (TextView) convertView.findViewById(R.id.text_downloadedmanage_bm_downloaded);
            holder.mTvNumTotal = (TextView) convertView.findViewById(R.id.text_downloadedmanage_bm_total);
            convertView.setTag(holder);
        }else{
            holder = (Holder) convertView.getTag();
        }
        DownloadedManageBean bean = mList.get(position);
        //图片
        String imagePath = bean.getImagePath();
        String imageUrl = ImageDownloader.Scheme.FILE.wrap(imagePath);
        PrintLog.d(TAG,"imageUrl:"+imageUrl);
//        Glide.with(mContext).load(imagePath).centerCrop().placeholder(mContext.getResources().getDrawable(R.drawable.img03)).crossFade().into(holder.mIv);
//        Glide.get(mContext).clearMemory();
        final Holder holder1 = holder;
        ImageLoader.getInstance().loadImage(imageUrl, new SimpleImageLoadingListener(){

            @Override
            public void onLoadingComplete(String imageUri, View view,
                                          Bitmap loadedImage) {
                super.onLoadingComplete(imageUri, view, loadedImage);
                holder1.mIv.setImageBitmap(loadedImage);
            }
        });

        //标题
        String courseName = bean.getCourseName();
        holder.mTvTitle.setText(courseName);
        //老师
        String teacher = bean.getTeacher();
        holder.mTvTeacher.setText(teacher);
        //缓存的数量
        String downloadedNumber = bean.getDownloadedNumber();
        String totalNumber = bean.getTotalNumber();
        holder.mTvNumdownloaded.setText(downloadedNumber);
        holder.mTvNumTotal.setText("/"+totalNumber);
        //缓存的大小
        String downloadedSize = bean.getDownloadedSize();
        return convertView;
    }

    class Holder {
        ImageView mIv;
        TextView  mTvTitle;
        TextView  mTvTeacher;
        TextView  mTvNumdownloaded;
        TextView  mTvNumTotal;
    }
}
